/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  let set = new Set()
  let maxLen = 0

  //左指针left：初始化为0
  let left = 0
  //遍历右指针 right 从 0~s.length
  for (let right = 0; right < s.length; right++) {
    //如果集合set中存在字符s[right]，就截去s[left]并移动左边界，直到s[right]在集合set中不存在
    while (set.has(s[right])) {
      set.delete(s[left])
      left++
    }
    //将字符s[right]放入集合set
    set.add(s[right])
    //更新最大长度
    maxLen = Math.max(maxLen, right - left + 1)
  }

  return maxLen
};